<?php

namespace App\Http\Models\Dances;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

/*
 * DancesFrom  课程表单验证
 *
 * rules() 角色验证规则
 * messages() 课程验证提示信息
 *
 * @author 鱼超
 * @email 870741306@qq.com
 * @date 2018/4/5
 * */

class DancesFrom extends FormRequest
{

    public function authorize() {
        return true;
    }

//    public function failedValidation($validator)
//    {
//        $error = $validator->errors();
//        echo "<pre>";
//        print_r($error);
//        exit;
//    }

    public function rules() {
        if (Auth::user()->users_status == 1) {
            $rules = [
                'name' => 'required|max:100',
                'begin_time' => 'required',
                'end_time' => 'required',
                'course_price' => 'required|numeric',
                'vip_price' => 'required|numeric',
                'teacher_id' => 'required|numeric',
                'address' => 'required',
                'images' => 'required',
                'contents' => 'required',
                'league_number' => 'required|numeric',
            ];
            if (isset($this->all()['types'])) {
                if ($this->all()['types'] == 0) {
                    return $rules;
                }
            } else {
                unset($rules['league_number']);
                return $rules;
            }
        }
        return [
            'name' => 'required|max:100',
            'begin_time' => 'required',
            'end_time' => 'required',
            'course_price' => 'required|numeric',
            'vip_price' => 'required|numeric',
            'address' => 'required',
            'images' => 'required',
            'contents' => 'required'
        ];
    }

    public function messages() {
        if (Auth::user()->users_status == 1) {
            $message = [
                'name.required' => "课程名称不能为空",
                'name.max' => "课程名称不能超过100个字符",
                'begin_time.required' => '开始时间不能为空',
                'end_time.required' => "结束时间不能为空",
                'vip_price.required' => "vip价格不能为空",
                'course_price.required' => "课程价格不能为空",
                'vip_price.numeric' => "vip价格格式错误",
                'course_price.numeric' => "课程价格格式错误",
                'address.required' => "上课地址不能为空",
                'images.required' => "课程封面不能为空",
                'contents.required' => '课程内容不能为空',
                'teacher_id.required' => '私教不能为空',
                'teacher_id.numeric' => '私教格式错误',
                'league_number.required' => '团课人数不能为空',
                'league_number.numeric' => '团课人数格式错误',
            ];
            if (isset($this->all()['types'])) {
                if ($this->all()['types'] == 0) {
                    return $message;
                }
            } else {
                unset($message['league_number.required']);
                unset($message['league_number.numeric']);
                return $message;
            }
        }
        return [
            'name.required' => "课程名称不能为空",
            'name.max' => "课程名称不能超过100个字符",
            'begin_time.required' => '开始时间不能为空',
            'end_time.required' => "结束时间不能为空",
            'vip_price.required' => "vip价格不能为空",
            'course_price.required' => "课程价格不能为空",
            'vip_price.numeric' => "vip价格格式错误",
            'course_price.numeric' => "课程价格格式错误",
            'address.required' => "上课地址不能为空",
            'images.required' => "课程封面不能为空",
            'contents.required' => '课程内容不能为空',
        ];
    }

}
